package CSCE428;


import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;


public class NFAState {
	Vector<TransitionNode> Transition;

	public int StateID;
	// Set of NFA state from which this state is constructed
	public HashSet<NFAState> NFAStates;
	// DFA state is accepting state if it is constructed from 
	// an accepting NFA state
	public boolean AcceptingState;

	public NFAState(){
		StateID=-1;
		AcceptingState=false;
	}

	public NFAState(int id){
		StateID=id;
		AcceptingState=false;
		NFAStates=new HashSet<NFAState>();
		Transition=new Vector<TransitionNode>();

	}

	public NFAState(HashSet<NFAState> NFAStateSet, int id){//for DFA
		NFAStates = NFAStateSet;
		StateID = id;
		Transition=new Vector<TransitionNode>();		
        //if the end of NFA state is accepted, the end of DFA state will be accepted too
		AcceptingState = false;
		Iterator iter=NFAStateSet.iterator();
		while(iter.hasNext()){
			if(((NFAState)(iter.next())).AcceptingState)
				AcceptingState = true;
		}
	}
	
	HashSet<NFAState> GetNFAState(){
		return NFAStates;
	}

	public void AddTransition(char chInput, NFAState State)
	{
		assert(State != null);
		Transition.add(new TransitionNode(chInput, State));
	}
	//Returns all transitions from this state on specific input
	public Vector<NFAState> GetTransition(char chInput)
	{
		Vector<NFAState> States=new Vector<NFAState>();
//		NFAState State = null;
//		Iterator iter = Transition.keySet().iterator();
//		
//		while(iter.hasNext()){
//			char key = (Character) iter.next();
//			if(key == chInput){
//				 State = Transition.get(key);
//				 if(State!=null){
//						States.add(State);
//					}
//			}
//			
//		}
//		Set entries = Transition.entrySet();
//		Iterator iter = entries.iterator();
//		while(iter.hasNext()){
//			Set temp = (Set) iter.next();
//			NFAState State = ((HashMap<Character, NFAState>) temp).get(chInput);
//			if(State!=null){
//				States.add(State);
//			}
//		}

		for (int i = 0; i < Transition.size(); i++)
		{
			
		  TransitionNode transition = Transition.elementAt(i);
		  char key = transition.value;
		  NFAState State = transition.nextState;
		  if(key==chInput && State!=null)
			  States.add(State);
		  
		}
		
		return States;
	}

}
